package com.yonyou.pmclouds.stagecheck.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.DocCenterProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.ProjectStatusCheckProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.plan.plandriven.rmiitf.IScheduleDriven;
import com.yonyou.pmclouds.plan.processor.ScheduleDrivenProcessor;
import com.yonyou.pmclouds.project.processor.ProjectBillUpdateForUpdate;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForDeleteProcessor;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.stagecheck.entity.StageCheckVO;
import com.yonyou.pmclouds.stagecheck.mapper.StageCheckMapper;
import com.yonyou.pmclouds.stagecheck.process.StageCheckAfterInsertFillInspectItemInfoProcess;
import com.yonyou.pmclouds.stagecheck.process.StageCheckAfterUpdateFillInspectItemInfoProcess;
import com.yonyou.pmclouds.stagecheck.process.StageCheckProjectLogPostProcess;
import com.yonyou.pmclouds.stagecheck.rmitf.StageCheckMaintain;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author chulf
 * @Date 2020/3/31
 * @Description
 **/
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = StageCheckMaintain.class)
public class StageCheckMaintainImpl implements StageCheckMaintain, IFlowApproveBuziService {

    @Autowired
    private ProjectStatusCheckProcessor<StageCheckVO> projectStatusCheckProcessor;
    @Autowired
    private StageCheckAfterInsertFillInspectItemInfoProcess stageCheckAfterInsertFillInspectItemInfoProcess;
    @Autowired
    private StageCheckProjectLogPostProcess stageCheckProjectLogPostProcess;
    @Autowired
    private StageCheckAfterUpdateFillInspectItemInfoProcess afterUpdateFillInspectItemInfoProcess;
    @Autowired
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<StageCheckVO> billCountFactory;
    @Autowired
    private ProjectBillUpdateForUpdate.ProjectBillForUpdateProcessorFactory<StageCheckVO> billForUpdateProcessorFactory;
    @Autowired
    private UpdateBillCountForDeleteProcessor.UpdateBillCountForDeleteProcessorFactory<StageCheckVO> billCountDeleteFactory;
    @Autowired
    private StageCheckMapper mapper;
    @Autowired
    private IScheduleDriven scheduleDriven;
    @Autowired
    private DocCenterProcessor docCenterProcessor;

    private static String[] UPDATE_FIELDS = {StageCheckVO.PK_SCHEDULEITEM,StageCheckVO.CHECK_DES,StageCheckVO.CHECK_DATE,StageCheckVO.BILL_STATUS,StageCheckVO.PK_PROJECTPART};

    @Override
    @PMApprove
    public StageCheckVO insert(StageCheckVO stageCheckVO) throws BusinessException {
        SingleInsertBpTemplate<StageCheckVO> bp = new SingleInsertBpTemplate<StageCheckVO>(mapper);

        // 更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<StageCheckVO>());

        bp.addBeforeProcessor(projectStatusCheckProcessor);
        //保存表体检查项选择的结果
        bp.addAfterProcessor(stageCheckAfterInsertFillInspectItemInfoProcess);
        //统计信息
        bp.addAfterProcessor(billCountFactory.getProcessor(BillTypeConst.STAGECHECK_BILLTYPE));
        //智能日志
        bp.addAfterProcessor(stageCheckProjectLogPostProcess);
        bp.insert(stageCheckVO);

        return stageCheckVO;
    }
    @Override
    @PMApprove(trigger = "commit")
    public StageCheckVO update(StageCheckVO acceptanceVO) throws BusinessException {
        SingleUpdateBpTemplate<StageCheckVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
        // 更新审计信息
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<StageCheckVO>());
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        //修改后修改表体检查项选择的结果
        bp.addAfterProcessor(afterUpdateFillInspectItemInfoProcess);

        //修改后修改项目单据信息
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.STAGECHECK_BILLTYPE));
        bp.update(acceptanceVO);
        return acceptanceVO;
    }


    public StageCheckVO approve(String pkStageCheck, byte billStatus) throws BusinessException {
        if (StringUtils.isEmpty(pkStageCheck)) {
            return null;
        }
        StageCheckVO stageCheckVO = mapper.getById(pkStageCheck);
        SingleUpdateBpTemplate<StageCheckVO> bp = new SingleUpdateBpTemplate<>(mapper, new String[]{StageCheckVO.BILL_STATUS});

        stageCheckVO.setBillstatus(billStatus);

        // 单据驱动项目状态变化
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.STAGECHECK_BILLTYPE));
        // 驱动计划
        bp.addAfterProcessor(new ScheduleDrivenProcessor<StageCheckVO>(scheduleDriven));
        bp.addAfterProcessor(docCenterProcessor);
        bp.update(stageCheckVO);

        return stageCheckVO;
    }


    public StageCheckVO reject(String pkStageCheck) throws BusinessException {
        if (StringUtils.isEmpty(pkStageCheck)) {
            return null;
        }
        StageCheckVO stageCheckVO = mapper.getById(pkStageCheck);
        SingleUpdateBpTemplate<StageCheckVO> bp = new SingleUpdateBpTemplate<>(mapper,  new String[]{StageCheckVO.BILL_STATUS});
        stageCheckVO.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.STAGECHECK_BILLTYPE));
        bp.update(stageCheckVO);
        return stageCheckVO;
    }


    @Override
    public SuperVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException {
        return approve(pk_bill, billStatus);
    }

    @Override
    public SuperVO rejectByPkBill(String pk_bill) throws BusinessException {
        return reject(pk_bill);
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        StageCheckVO stageCheckVO = mapper.getById(pk_bill);
        return stageCheckVO;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.STAGECHECK_BILLTYPE;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<StageCheckVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(mapper);
        deleteBpTemplate.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.STAGECHECK_BILLTYPE));
        return deleteBpTemplate.delete((StageCheckVO) businessVO);
    }

    @Override
    public Object getMapper() {
        return mapper;
    }
}
